package com.haixue.android.academy.view;

import android.academy.haixue.com.haixue.R;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by ren on 2015/6/3 0003.
 */
public class TitleView extends RelativeLayout implements View.OnClickListener{

    private boolean isOpen = false; //true 关闭，false 打开
    private OnTitleClickListener onTitleClickListener;
    private LinearLayout ll_left;
    private ImageView iv_left;
    private TextView tv_left;
    private RelativeLayout ll_center;
    private RadioGroup rg_container_title_bar;
    private LinearLayout ll_select_category;
    private TextView tv_category_title;
    private ImageView iv_arrow;
    private LinearLayout ll_right;
    private ImageView iv_right;
    private TextView tv_right;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TitleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public TitleView(Context context) {
        super(context);
        init();
    }

    public void setOnTitleClickListener(OnTitleClickListener onTitleClickListener) {
        this.onTitleClickListener = onTitleClickListener;
    }

    private void init() {
        int title_height = getResources().getDimensionPixelSize(R.dimen.title_height);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, title_height);
        lp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        View inflate = View.inflate(getContext(), R.layout.layout_title, null);
        initViews(inflate);
        addView(inflate, lp);
        initListener();
    }

    private void initListener() {
        ll_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        iv_right.setOnClickListener(this);
        ll_select_category.setOnClickListener(this);
    }

    private void initViews(View view) {
        ll_left = (LinearLayout) view.findViewById(R.id.ll_left);
        ll_select_category = (LinearLayout) view.findViewById(R.id.ll_select_category);
        ll_right = (LinearLayout) view.findViewById(R.id.ll_right);
        ll_center = (RelativeLayout) view.findViewById(R.id.ll_center);
        iv_left = (ImageView) view.findViewById(R.id.iv_left);
        iv_arrow = (ImageView) view.findViewById(R.id.iv_arrow);
        iv_right = (ImageView) view.findViewById(R.id.iv_right);
        tv_left = (TextView) view.findViewById(R.id.tv_left);
        tv_right = (TextView) view.findViewById(R.id.tv_right);
        tv_category_title = (TextView) view.findViewById(R.id.tv_category_title);
        rg_container_title_bar = (RadioGroup) view.findViewById(R.id.rg_container_title_bar);
        initDatas();
    }

    private void initDatas() {

    }


    public void showSelectCategoryTitle(){
        ll_left.setVisibility(GONE);
        ll_right.setVisibility(GONE);
        rg_container_title_bar.setVisibility(GONE);
        ll_select_category.setVisibility(VISIBLE);
    }
    public void showRadioGroupTitle(){
        tv_left.setVisibility(GONE);
        tv_right.setVisibility(GONE);
        ll_left.setVisibility(VISIBLE);
        ll_right.setVisibility(VISIBLE);
        ll_select_category.setVisibility(VISIBLE);
        rg_container_title_bar.setVisibility(VISIBLE);
    }

    public void showCommonTitle(){
        ll_left.setVisibility(VISIBLE);
        ll_right.setVisibility(VISIBLE);
        tv_left.setVisibility(VISIBLE);
        tv_right.setVisibility(GONE);
    }


    @Override
    public void onClick(View view) {
        if (onTitleClickListener==null)
            return;
        switch (view.getId())
        {
            case R.id.ll_left:
                onTitleClickListener.onLeftClick(view);
                break;
            case R.id.ll_select_category:
                onTitleClickListener.onSelectCategoryClick(view);
                rotateArrow();
                break;
            case R.id.iv_right:
            case R.id.tv_right:
                onTitleClickListener.onRightClick(view);
                break;

        }
    }

    private void rotateArrow() {
//        iv_arrow.clearAnimation();
//        if (isOpen){
//            Animation rotateClose = AnimationUtils.loadAnimation(getContext(),
//                    R.anim.animation_rotate_closed);
//
//            rotateClose.setFillEnabled(true);
//
//            rotateClose.setFillAfter(true);
//
//
//            iv_arrow.startAnimation(rotateClose);
//        }else{
//            Animation rotateOpen = AnimationUtils.loadAnimation(getContext(),
//                    R.anim.animation_rotate_open);
//
//            rotateOpen.setFillEnabled(true);
//
//            rotateOpen.setFillAfter(true);
//
//            iv_arrow.startAnimation(rotateOpen);
//        }
//        isOpen=!isOpen;

    }

    public static interface OnTitleClickListener{
        void onLeftClick(View view);
        void onRightClick(View view);
        void onSelectCategoryClick(View view);
    }
}
